home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / csim / source.lha / source / C++SIM / gnu_thread.cc < prev    next >
C/C++ Source or Header  |  1993-06-14  |  2KB  |  99 lines

  1. /*
  2.  * Copyright (C) 1993
  3.  *
  4.  * Department of Computing Science,
  5.  * The University,
  6.  * Newcastle upon Tyne,
  7.  * UK.
  8.  */
  9.  
  10. #include <iostream.h>
  11. #include <sys/types.h>
  12.  
  13. #define _INIT_
  14. #include <gnulwp.h>
  15.  
  16. #ifndef GNUTHREAD_H_
  17. #include "gnu_thread.h"
  18. #endif
  19.  
  20.  
  21. long GNU_Thread::base_key = 0;
  22. boolean GNU_Thread::DoWait = true;
  23. boolean GNU_Thread::SuspendMain = false;
  24. int GNU_Thread::count = 0;
  25. const int GNU_Thread::MaxPriority = 7;
  26.  
  27.  
  28. GNU_Thread::GNU_Thread (int prio)
  29. {
  30.     to_wait = 0;
  31.     caddr_t p1;
  32.  
  33.     initlp(MaxPriority);
  34.     p1 = (caddr_t) this;
  35.     thread_key = base_key++;
  36.     my_block = creatp(prio, GNU_Thread::Execute, 8000, 0, 0, p1);
  37. }
  38.  
  39. GNU_Thread::~GNU_Thread () {}
  40.  
  41. void GNU_Thread::Execute (int dummy1, char** dummy2, GNU_Thread* p1)
  42. {
  43.     // Do this to remove compiler warnings.
  44.     dummy1 = 0;
  45.     dummy2 = 0;
  46.  
  47.     if ((p1->thread_key == 1) && (Self()->Current_Thread() == 0))
  48.     {
  49.     signals(p1->to_wait);
  50.     p1->Body();
  51.     }
  52.     else
  53.     {
  54.     p1->to_wait = creats(0);
  55.     p1->Suspend();
  56.     p1->Body();
  57.     }
  58. }
  59.     
  60. void GNU_Thread::Suspend ()
  61. {
  62.     if ((thread_key == 0) && (Self()->Current_Thread() == 0) && (count < 2))
  63.     count++;
  64.  
  65.     if ((count == 2) && (!SuspendMain))
  66.     {
  67.     SuspendMain = true;
  68.     struct sem* p = creats(0);
  69.     waits(p);
  70.     }
  71.  
  72.     if ((thread_key == 1) && (Self()->Current_Thread() == 0))
  73.     {
  74.     if (DoWait)
  75.     {
  76.         DoWait = false;
  77.         waits(to_wait);
  78.     }
  79.     }    
  80.     else
  81.     waits(to_wait);
  82. }
  83.  
  84. void GNU_Thread::Resume ()
  85. {
  86.     if ((thread_key == 1) && (Self()->Current_Thread() == 0))
  87.     {
  88.     readyp(my_block);
  89.     to_wait = creats(0);
  90.     Suspend();
  91.     }
  92.     else
  93.         signals(to_wait);
  94. }
  95.  
  96. long GNU_Thread::Current_Thread () const { return thread_key; }
  97.  
  98. void GNU_Thread::Initialize () { initlp(1); }
  99.